import { defineConfig, loadEnv, type UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslint from 'vite-plugin-eslint'
import { resolve } from 'path'
import ElementPlus from 'unplugin-element-plus/vite'

/**
 * 拼接路径
 * @param dir 拼接的目录名称
 */
function pathResolve(dir: string): string {
  return resolve(process.cwd(), '.', dir)
}

// https://vitejs.dev/config/
export default ({ mode }): UserConfig => {
  const { VITE_PREFIX, VITE_BASE_URL } = loadEnv(mode, process.cwd(), '')

  return defineConfig({
    plugins: [vue(), eslint(), ElementPlus({})],
    resolve: {
      /** 路径别名 */
      alias: [{ find: '@', replacement: pathResolve('src') }]
    },
    server: {
      proxy: {
        [VITE_PREFIX]: {
          target: VITE_BASE_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "./src/styles/variables.scss" as *;'
        }
      }
    }
  })
}
